package com.zkt.subject.domain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson2.JSON;
import com.zkt.subject.common.enums.CategoryTypeEnum;
import com.zkt.subject.domain.convert.SubjectLabelConverter;
import com.zkt.subject.domain.entity.SubjectLabelBO;
import com.zkt.subject.domain.service.SubjectLabelDomainService;
import com.zkt.subject.infra.basic.entity.SubjectCategory;
import com.zkt.subject.infra.basic.entity.SubjectLabel;
import com.zkt.subject.infra.basic.entity.SubjectMapping;
import com.zkt.subject.infra.basic.service.ISubjectCategoryService;
import com.zkt.subject.infra.basic.service.ISubjectLabelService;
import com.zkt.subject.infra.basic.service.ISubjectMappingService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2024/9/30
 * @description 题目标签领域服务
 **/

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
	
	@Resource
	private ISubjectLabelService subjectLabelService;
	
	@Resource
	private ISubjectMappingService subjectMappingService;
	
	@Resource
	private ISubjectCategoryService subjectCategoryService;
	
	/**
	 * 新增标签
	 *
	 * @param subjectLabelBO 新增的标签数据
	 * @return 成功与否
	 */
	@Override
	public Boolean add(SubjectLabelBO subjectLabelBO) {
		if (log.isInfoEnabled()) {
			log.info("SubjectLabelDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectLabelBO));
		}
		SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
		return subjectLabelService.save(subjectLabel);
	}
	
	/**
	 * 更新标签
	 *
	 * @param subjectLabelBO 更新的标签数据
	 * @return 成功与否
	 */
	@Override
	public Boolean update(SubjectLabelBO subjectLabelBO) {
		if (log.isInfoEnabled()) {
			log.info("SubjectLabelDomainServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
		}
		SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
		return subjectLabelService.updateById(subjectLabel);
	}
	
	/**
	 * 删除标签
	 *
	 * @param subjectLabelBO 需要删除的标签的数据
	 * @return 成功与否
	 */
	@Override
	public Boolean delete(SubjectLabelBO subjectLabelBO) {
		if (log.isInfoEnabled()) {
			log.info("SubjectLabelDomainServiceImpl.delete.bo:{}", JSON.toJSONString(subjectLabelBO));
		}
		SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
		return subjectLabelService.removeById(subjectLabel);
	}
	
	/**
	 * 根据分类查询标签
	 *
	 * @param subjectLabelBO 查询条件
	 * @return 查询结果
	 */
	@Override
	public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
		List<Long> categoryIdList = new LinkedList<>();
		if (ObjUtil.isNotNull(subjectLabelBO.getCategoryId())) {
			categoryIdList.add(subjectLabelBO.getCategoryId());
			// 当前分类是1级分类，则查询所有标签
			SubjectCategory subjectCategory = subjectCategoryService.getById(subjectLabelBO.getCategoryId());
			if (CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()) {
				categoryIdList = subjectCategoryService.lambdaQuery()
						.eq(SubjectCategory::getParentId, subjectLabelBO.getCategoryId())
						.list()
						.stream()
						.map(SubjectCategory::getId)
						.toList();
			}
		}
		
		return SubjectLabelConverter.INSTANCE.convertLabelToBoList(subjectLabelService.lambdaQuery()
				.in(CollUtil.isNotEmpty(categoryIdList), SubjectLabel::getCategoryId, categoryIdList)
				.list());
	}
	
	/**
	 * 根据id查询标签
	 *
	 * @param subjectLabelIds id列表
	 * @return 成功与否
	 */
	@Override
	public List<SubjectLabelBO> queryLabelByIds(List<Long> subjectLabelIds) {
		List<SubjectLabel> subjectLabelList = subjectLabelService.listByIds(subjectLabelIds);
		
		return SubjectLabelConverter.INSTANCE.convertLabelToBoList(subjectLabelList);
	}
}
